/**
 * @date 2006-9-15
 */

package org.nanhill.commons.xml;

import static org.nanhill.commons.lang.StringConstants.GT;
import static org.nanhill.commons.lang.StringConstants.LT;
import static org.nanhill.commons.lang.StringConstants.QUOT;
import static org.nanhill.commons.xml.XmlConstants.AMP;
import static org.nanhill.commons.xml.XmlConstants.AMP_ESCAPE_ARRAY;
import static org.nanhill.commons.xml.XmlConstants.APOS;
import static org.nanhill.commons.xml.XmlConstants.APOS_ESCAPE_ARRAY;
import static org.nanhill.commons.xml.XmlConstants.GT_ESCAPE_ARRAY;
import static org.nanhill.commons.xml.XmlConstants.LT_ESCAPE_ARRAY;
import static org.nanhill.commons.xml.XmlConstants.QUOT_ESCAPE_ARRAY;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-9-15
 */
public class XmlKit {

	/**
	 * Escape xml constants characters
	 * 
	 * @param v
	 * @return
	 */
	public static String xmlEscape(final String v) {
		StringBuilder builder = new StringBuilder(v.length());
		for (int i = 0; i < v.length(); i++) {
			char ch = v.charAt(i);
			switch (ch) {
			case LT:
				builder.append(LT_ESCAPE_ARRAY);
				break;
			case GT:
				builder.append(GT_ESCAPE_ARRAY);
				break;
			case AMP:
				builder.append(AMP_ESCAPE_ARRAY);
				break;
			case QUOT:
				builder.append(QUOT_ESCAPE_ARRAY);
				break;
			case APOS:
				builder.append(APOS_ESCAPE_ARRAY);
				break;
			default:
				builder.append(ch);
			}
		}
		return builder.toString();
	}

}
